package com.example.tweetfeed;

import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.view.View;

public class LoadImageTask extends AsyncTask<String, Void, Bitmap>{
	private LoadImageCallback mCaller = null;
	private Tweet mTweet = null;
	private View mView = null;
	public LoadImageTask(LoadImageCallback caller, Tweet tw, View v) {
		super();
		mCaller = caller;
		mTweet = tw;
		mView = v;
	}

	public interface LoadImageCallback {
		void onLoadImage(Tweet tw, View v, Bitmap img);
	}

	@Override
	protected Bitmap doInBackground(String... params) {
		Bitmap img = null;
		String dir = params[0];
		try {
			URL url = new URL(dir);
			HttpURLConnection conn = (HttpURLConnection) url.openConnection();
			InputStream is = conn.getInputStream();
			img = BitmapFactory.decodeStream(is, null, new BitmapFactory.Options());
		} catch(Exception e) {
			e.printStackTrace();
		} finally {
			if(mCaller != null) {
	        	mCaller.onLoadImage(mTweet, mView, img);
	        }
		}
		return null;
	}
}
